"use strict";

const md5 = require("md5");
const jwt = require("jsonwebtoken");
const BasicController = require("./basic");
// 固定的盐
const HashSalt = "low-code";
const registerRule = {
      password: { type: "string" },
      username: { type: "string" },
      email: { type: "email" },
      captcha: { type: "string" },
};

class UserController extends BasicController {
      // 注册
      async register() {
            try {
                  // 基础校验
                  this.ctx.validate(registerRule);
            } catch (e) {
                  return this.error("参数校验失败", e.errors);
            }
            const { request, session, model } = this.ctx;
            const { password, username, email, captcha } = request.body;
            // session中获取验证码
            const sessionCaptcha = session.captcha.toUpperCase();
            // 验证码校验
            if (sessionCaptcha !== captcha.toUpperCase()) {
                  return this.error("验证码错误");
            }
            // 校验邮箱
            if (await this.getUserByEmail(email)) {
                  return this.error("邮箱已存在");
            }

            // 入库
            let user = await model.User.create({
                  password: md5(password + HashSalt),
                  username,
                  email,
            });
            // 入库成功后会返回用户信息，如果有id表示入库成功
            if (user._id) {
                  this.message("注册成功");
            }
      }

      // 登录
      async login() {
            const { request, session, model } = this.ctx;
            const { password, email, captcha, emailCode } = request.body;
            // session中获取验证码
            const sessionCaptcha = session.captcha.toUpperCase();
            const sessionEmailCode = session.emailCode;
            // 验证码校验
            if (sessionCaptcha !== captcha.toUpperCase()) {
                  return this.error("验证码错误");
            }

            // 邮箱验证码
            if (emailCode !== sessionEmailCode) {
                  return this.error("邮箱验证码错误");
            }

            // 获取用户，通过email和password
            const user = await model.User.findOne({
                  email,
                  password: md5(password + HashSalt),
            });
            // 用户存在
            if (!user) {
                  return this.error("用户名或密码错误");
            }
            // jwt.sign(加密信息，secret，其他配置【当前配置为1h时后过期】)
            const token = jwt.sign(
                  {
                        username: user.username,
                        email: email,
                        id: user._id,
                  },
                  this.app.config.jwt.secret,
                  {
                        expiresIn: "1h",
                  }
            );
            this.success({ token, email, username: user.username });
      }

      async getUserByEmail(email) {
            const user = await this.ctx.model.User.findOne({ email });
            return user;
      }

      async verify() { }

      async info() {
            // 获取自己的用户信息
            const email = this.ctx.state.email;
            // 请求的是其他用户信息
            const user = await this.getUserByEmail(email);
            this.success(user);
      }
}

module.exports = UserController;
